
 /**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */
package com.sys.midware.redis.core.operation.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.sys.midware.redis.core.RedisFactory;
import com.sys.midware.redis.core.operation.AbstractOperations;
import com.sys.midware.redis.core.operation.HashOperations;
import com.sys.midware.redis.exceptions.BRedisException;

import redis.clients.jedis.Jedis;

/**
 * <b>Description：</b> TODO <br/>
 * <b>ClassName：</b> HashOperationsImpl <br/>
 * <b>@author：</b> jackyshang <br/>
 * <b>@date：</b> 2016年8月22日 下午4:11:23 <br/>
 * <b>@version: </b>  <br/>
 */
public class HashOperationsImpl  extends AbstractOperations implements HashOperations{

    private HashOperationsImpl() {
    };

    private static HashOperationsImpl instance;
    
    /**
     * 获取单例工厂
     * @return
     * @throws Exception
     */
    public static HashOperationsImpl getInstance() {
        if (instance != null)
            return instance;

        synchronized (HashOperationsImpl.class) {
            if (instance == null) {
                instance = new HashOperationsImpl();
            }
            return instance;
        }
    }
    
    @Override
    public <H> void delete(H key, Object... hashKeys) {
        Jedis jedis = getClient();
        try {
//            Map<byte[], byte[]> map = jedis.hgetAll(rawKey(key));
            //删除
            for (Object mkey :hashKeys ) {
//                map.remove(rawKey(obj));
                
                jedis.hdel(rawKey(key), rawKey(mkey));
            }
//            if(map!=null){
//                //设置
//                jedis.hmset(rawKey(key),map);
//            }
//           
//            jedis.hdel(rawKey(key), rawKey(key))
            
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <H> Boolean hasKey(H key, Object hashKey) {
        Jedis jedis = getClient();
        try {
            Map<byte[], byte[]> map = jedis.hgetAll(rawKey(key));
           return  map==null? false : map.containsKey(rawKey(hashKey));
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <H, HV> HV get(H key, Object hashKey) {
        Jedis jedis = getClient();
        try {
//            Map<byte[], byte[]> map = jedis.hgetAll(rawKey(key));
            byte[] v =  jedis.hget(rawKey(key), rawKey(hashKey));
            HV hv=  deserializeValue( v);
           return hv;
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    
    @Override
    public <H, HK, HV> List<HV> getByKeys(H key, Collection<HK> hashKeys) {
        List<HV> reList = new ArrayList<HV>();
        Jedis jedis = getClient();
        try {
            Map<byte[], byte[]> map = jedis.hgetAll(rawKey(key));
            
            for(HK hk : hashKeys){
                HV hv=  deserializeValue( map.get(rawKey(hk)));
                reList.add(hv);
            }
           return reList;
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public <H, HK> Set<HK> getAllKeys(H key) {
        Jedis jedis = getClient();
        Set<HK> reList = new HashSet<HK>();
        try {
            Map<byte[], byte[]> map = jedis.hgetAll(rawKey(key));
            if (map != null) {
                for (Entry<byte[], byte[]> map1 : map.entrySet()) {
                    HK hk = (HK) deserializeKey(map1.getKey());
                    reList.add(hk);
                }
            }
            return reList;
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public <H, HV> List<HV> getAllValues(H key) {
        Jedis jedis = getClient();
        List<HV> reList = new ArrayList<HV>();
        try {
            Map<byte[], byte[]> map = jedis.hgetAll(rawKey(key));
            if (map != null) {
                for (Entry<byte[], byte[]> map1 : map.entrySet()) {
                    HV v = (HV) deserializeValue(map1.getValue());
                    reList.add(v);
                }
            }
            return reList;
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <H> int size(H key) {
        Jedis jedis = getClient();
        try {
            Map<byte[], byte[]> map = jedis.hgetAll(rawKey(key));
            return map ==null ? 0 :map.size();
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public <H, HK, HV> Map<HK, HV> getMap(H key) {
       Jedis jedis = getClient();
      Map<HK, HV> retMap = new HashMap<HK, HV>();
      try {
          Map<byte[], byte[]> map = jedis.hgetAll(rawKey(key));
          if (map != null) {

              for (Entry<byte[], byte[]> map1 : map.entrySet()) {
                  HK f = (HK) deserializeKey(map1.getKey());
                  HV v = (HV) deserializeValue(map1.getValue());
                  retMap.put(f, v);
              }
          }
          return retMap;
      } catch (Exception e) {
          throw new BRedisException(e);
      } finally {
          RedisFactory.close(jedis);
      }
        
    }

    @Override
    public <H, HK, HV> void put(H key, HK hashKey, HV value) {
       Jedis jedis = getClient();
      try {
          jedis.hset(rawKey(key), rawKey(hashKey),rawValue(value));
      } catch (Exception e) {
          throw new BRedisException(e);
      } finally {
          RedisFactory.close(jedis);
      }
        
    }
    @Override
    public <H, HK, HV> void putAll(H key, Map<? extends HK, ? extends HV> m) {
        Jedis jedis = getClient();
        try {
            Map<byte[], byte[]> map = new HashMap<>();
            
            for(Entry<? extends HK, ? extends HV>  e :m.entrySet()){
                map.put( rawKey(e.getKey()), rawValue(e.getValue()));
            }
            jedis.hmset(rawKey(key),map);
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
        
    }

  
}
